﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using TpLogger;

namespace TpBaseObjects.Base
{
	public class ProjektArSheet
	{
		#region Members

		Worksheet mySheet;
		int nextInvoiceNumber = 0;

		#endregion

		#region Constructor

		public ProjektArSheet(Worksheet sh)
		{
			this.mySheet = sh;
		}

		#endregion

		#region Properties

		public int NewInvoiceNumber
		{
			get
			{
				return this.nextInvoiceNumber;
			}
			set
			{
				this.nextInvoiceNumber = value;
			}
		}

		#endregion

		#region Functions

		public int GetNextFreeRowNumber(int jahr, int monat, Logger log)
		{

			int ret = 0;
			int currentRow = 2;
			int currentMonth = 1;
			int freeRow = 2;
			bool weiter = false;
			Range r1;

            try {
                while (weiter == false & currentRow < 10000) {
                    r1 = mySheet.Rows[currentRow];
                    InvoiceNumberRow row = new InvoiceNumberRow(r1);
                    Range r2 = mySheet.Rows[currentRow + 1];
                    InvoiceNumberRow nextRow = new InvoiceNumberRow(r2);
                    Range rp = mySheet.Rows[currentRow - 1];
                    InvoiceNumberRow prevRow = new InvoiceNumberRow(rp);
                    if (row.IsFree & currentRow == 2) {
                        NewInvoiceNumber = 1;
                        return freeRow;
                    }

                    if (row.IsFree & currentMonth == monat & !nextRow.IsSummenZeile) {
                        NewInvoiceNumber = prevRow.RechnungsNummer + 1;
                        //row.RechnungsNummer = NewInvoiceNumber;
                        return currentRow;
                    }

                    if (row.IsSummenZeile) {
                        if (currentMonth < 12) {
                            currentMonth = row.Monat + 1;
                        }
                    }


                    if (!row.IsFree & currentMonth == monat & nextRow.IsSummenZeile) {
                        if (((r2.Row - r1.Row) == 1)) {
                            Range r3 = mySheet.Rows[r1.Row];
                            r3.Copy();
                            r3.Select();
                            r3.Insert(Shift: XlInsertShiftDirection.xlShiftDown);
                            freeRow = r3.Row;
                            Range r4 = mySheet.Rows[freeRow];
                            InvoiceNumberRow newRow = new InvoiceNumberRow(r4);
                            newRow.ResetRow();
                            NewInvoiceNumber = prevRow.RechnungsNummer + 1;
                            //newRow.RechnungsNummer = NewInvoiceNumber;
                            return freeRow;
                        }
                    }
                    currentRow += 1;
                }
            } catch (Exception ex) {
                log.Add("GetNextFreeRowNumber", ex);
            }

			return ret;
		}

		#endregion

	}
}
